import tkinter as tk
import random

class GuessNumberGame:
    def __init__(self, master):
        self.master = master
        self.master.title("猜数游戏")

        self.secret_number = random.randint(1, 100)
        self.guesses_left = 10

        self.instructions_label = tk.Label(master, text="猜一个1到100之间的数字，你有10次机会。")
        self.instructions_label.pack()

        self.guess_entry = tk.Entry(master)
        self.guess_entry.pack()

        self.submit_button = tk.Button(master, text="提交", command=self.check_guess)
        self.submit_button.pack()

        self.feedback_label = tk.Label(master, text="")
        self.feedback_label.pack()

    def check_guess(self):
        guess = int(self.guess_entry.get())
        self.guess_entry.delete(0, tk.END)

        if self.guesses_left == 0:
            self.feedback_label.config(text="很遗憾，次数用完，正确答案是：{}".format(self.secret_number))
        elif guess < self.secret_number:
            self.feedback_label.config(text="太小了！还剩{}次机会".format(self.guesses_left - 1))
            self.guesses_left -= 1
        elif guess > self.secret_number:
            self.feedback_label.config(text="太大了！还剩{}次机会".format(self.guesses_left - 1))
            self.guesses_left -= 1
        else:
            self.feedback_label.config(text="恭喜你，猜对了！")
            self.guesses_left -= 1

root = tk.Tk()
game = GuessNumberGame(root)
root.mainloop()
